package YAPserver;

import YAPenvironment.YAPconstant;
import YAPenvironment.YAPconstant.Command;

/**
 * A runnable object which checks if a client is still connected to the server in specified time intervals.
 * These time intervals are declared in {@link YAPconstant}
 */
public class CheckConnection extends Thread{
	
	private ClientHandler cH;
	
	/**
	 * Constructs the check connection object.
	 * Checks the connection for a client every x seconds
	 * @param cH
	 */
	CheckConnection(ClientHandler cH){
		this.cH=cH;
	}
	
	@Override
	public void run(){
		while(cH.run){
			if(!cH.isConnected()){
				//The Client disconnected
				System.out.println(cH.getSocket()+" :***TIMED OUT***");
				cH.disconnect();
			}
			cH.setConnected(false);
			try{
				cH.writeMessage(Command.isAliveServer.toString()+YAPconstant.C_SEPARATOR);
				Thread.sleep(YAPconstant.C_ALIVE_INTERVAL); 
			}catch(Exception e){
				if(YAPconstant.C_DEBUG){ e.printStackTrace(); }
			}
		}
	}	
}
